package com.sinotrans.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinotrans.common.constant.Constant;
import com.sinotrans.common.exception.BusinessException;
import com.sinotrans.common.util.Utils;
import com.sinotrans.demo.entity.UserEntity;
import com.sinotrans.demo.repository.UserRepository;
import com.sinotrans.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository userRepository;

	@Cacheable(cacheNames = Constant.ShortTimeCache)
	@Override
	public UserEntity login(String username, String password) throws Exception {
		UserEntity param = new UserEntity();
		param.setUsername(username);
		QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<UserEntity>();
		queryWrapper.setEntity(param);
		UserEntity result = userRepository.getOne(queryWrapper);
		if (result == null) {
			throw new BusinessException("user.not.registered");
		}
		if (!Utils.isEquals(result.getPassword(), password)) {
			throw new BusinessException("user.name.password.error");
		}
		return result;
	}

}
